﻿//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//    Crolengi, this program (a part of program) is gameengine based on C++\Directx
//    Copyright (C) 2008 Pashinin Anton Alekseevich (Crol)
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//    (INCLUDING NEGLIGENCE OR OTHERWISE) 
//    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//    Contacts: 
//    Mail: crolengi@gmail.com
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef  BaseUI_h
#define  BaseUI_h


#include "include\\Utils\\DrawingFunctions.h"
#include "include\\Utils\\defines01.h"


//#################################################################################################################
///// 
/////   :: РљР›РђРЎРЎ :: Р‘Р°Р·РѕРІС‹Р№ РєР»Р°СЃСЃ РґР»СЏ СЌР»РµРјРµРЅС‚РѕРІ UI
///// 
//#################################################################################################################
class CLREngi_UI_BaseUI
{
  protected:
	struct CUSTOMVERTEX
	{
		float x, y, z,w;//, rhw; // The transformed position for the vertex
		D3DCOLOR color;
		 FLOAT tu, tv; 
	};
	#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZW | D3DFVF_DIFFUSE|D3DFVF_TEX1 )

  public:
	LPDIRECT3DDEVICE9        *Device;
	D3DCOLOR                 BackColor,TextColor,BgColor;
	
	LPDIRECT3DVERTEXBUFFER9  *g_pVB;

	LPD3DXFONT               *Font; 
	LPD3DXFONT               *FontName; 

	LPDIRECT3DTEXTURE9      g_pTexture;

	

	int                      pos_x,  // РєРѕРѕСЂРґР°РЅР°С‚С‹ ui СЌР»РµРјРµРЅС‚Р°            
		                     pos_y,              
							 
							 width,  // РіР°Р±Р°СЂРёС‚С‹ ui СЌР»РµРјРµРЅС‚Р°
							 height;
	                         // (РїРѕР»РѕР¶РµРЅРёРµ Р·Р°РґР°РµС‚СЃСЏ РѕС‚РЅРѕСЃРёС‚РµР»СЊРЅРѕ РєР°РјРµСЂС‹, РїСЂРёС‡РµРј РѕС‚ СЃРµСЂРµРґРёРЅС‹ СЌРєСЂР°РЅР°)

	bool                     selected;
	bool                     allow_shift;

	CRLEngi_string           Name;
	CRLEngi_string           Text;

	POINT       last_mouse_point;
	POINT       curr_mouse_point;
	POINT       last_mouse_pitch;

	D3DXMATRIX             matWorld;      // view - position РјР°С‚СЂРёС†Р°, Р·Р°РґР°РµС‚СЃСЏ Р·Р°СЂР°РЅРµРµ
	D3DXMATRIX             Transform;     // РјР°С‚СЂРёС†Р° РґР»СЏ СЃР±РѕСЂР° РІСЃРµС… С‚СЂР°РЅСЃС„РѕСЂРјР°С†РёР№
	D3DXMATRIX             Buff;          // РјР°С‚СЂРёС†Р° РґР»СЏ РІСЂРµРјРµРЅРЅРѕРіРѕ С…СЂР°РЅРµРЅРёСЏ Р·РЅР°С‡РµРЅРёР№ РјР°С‚СЂРёС†

	 bool                  visible;        // РІРёРґРёРјС‹Р№ Р»Рё СЌР»РµРјРµРЅС‚ СЃРµР№С‡Р°СЃ
	 bool                  ZBuffer_Enable; // РЅСѓР¶РЅРѕ Р»Рё РёСЃРїР»СЊР·РѕРІР°С‚СЊ z Р±СѓС„РµСЂ

	 LPD3DXSPRITE          Text_Sprite; // СЃРїСЂР°Р№С‚ РґР»СЏ РІС‹РІРѕРґР° С‚РµРєСЃС‚Р°
//#################################################################################################################
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Render(){

	        Draw_Rect(pos_x-5180,pos_y,10052,width,height);
			Draw_Text(pos_x-5000,pos_y,10000,10,10, Text.getChars(),&TextColor);
			Draw_Text(pos_x-5000,pos_y+200,10000,10,10, Name.getChars(),&TextColor);
								
				if ((!allow_shift)&&(selected)){
					drawLine(D3DXVECTOR3(pos_x-1,       pos_y-1        ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y-1        , 0.0f),Device);
					drawLine(D3DXVECTOR3(pos_x-1,       pos_y+height+1 ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y+height+1 , 0.0f),Device);
					drawLine(D3DXVECTOR3(pos_x-1,       pos_y-1        ,0.0f),D3DXVECTOR3(pos_x-1       ,pos_y+height+1 , 0.0f),Device);
					drawLine(D3DXVECTOR3(pos_x+width+1, pos_y-1        ,0.0f),D3DXVECTOR3(pos_x+width+1 ,pos_y+height+1 , 0.0f),Device);
				}
				else
				if (selected){
					drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y+1       -last_mouse_pitch.y ,  0.0f),Device);
					drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y-height+1 -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
					drawLine(D3DXVECTOR3(curr_mouse_point.x+1      +last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x+1       +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
					drawLine(D3DXVECTOR3(curr_mouse_point.x-width-1+last_mouse_pitch.x, curr_mouse_point.y+1        -last_mouse_pitch.y ,0.0f),D3DXVECTOR3(curr_mouse_point.x-width-1 +last_mouse_pitch.x, curr_mouse_point.y-height-1-last_mouse_pitch.y ,  0.0f),Device);
				}
				Render_More();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////  С„СѓРЅРєС†РёСЏ РєРѕС‚РѕСЂР°СЏ РјРѕР¶РµС‚ СЂР°СЃС€РёСЂСЏС‚СЊ СЃС‚Р°РЅРґР°СЂС‚РЅСѓСЋ РІ РєР»Р°СЃСЃР°С… РїРѕС‚РѕРјРєР°С…
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Render_More(){}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// С„СѓРЅРєС†РёСЏ РѕС‚СЂРёСЃРѕРІРєРё РІ РѕРїРµСЂРµРґРµР»РµРЅРЅРѕР№ С‚РѕС‡РєРµ СЃРјР°СЃС€С‚Р°Р±РёСЂРѕРІР°РЅРЅРѕРіРѕ РїСЂСЏРјРѕСѓРіРѕР»СЊРЅРёРєР°
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void                   Draw_Rect(float x, // x position
			                     float y, // y position
			                     float z, // z position
			                     float w, // width of rect
			                     float h) // height of rect
{ 
     (*Device)->SetRenderState( D3DRS_LIGHTING, FALSE );
     if (!ZBuffer_Enable) 
        (*Device)->SetRenderState( D3DRS_ZENABLE, FALSE );
  
	  // С‚СЂР°РЅСЃС„РѕСЂРјР°С†РёРё РґР»СЏ РїСЂСЏРјРѕСѓРіРѕР»СЊРЅРёРєР°
	            D3DXMatrixIdentity(&Transform);	

	            D3DXMatrixIdentity(&Buff);
				D3DXMatrixRotationX(&Buff,D3DX_PI);
				D3DXMatrixMultiply(&Transform,&Transform,&Buff);

				D3DXMatrixScaling(&Buff,w,h,1);
				D3DXMatrixMultiply(&Transform,&Transform,&Buff);

				D3DXMatrixTranslation(&Buff,x,y,z);
				D3DXMatrixMultiply(&Transform,&Transform,&Buff);

				D3DXMatrixMultiply(&Transform,&Transform,&matWorld);

				(*Device)->SetTransform( D3DTS_WORLD, &Transform);

	            (*Device)->SetTexture( 0, NULL );	
				(*Device)->SetTexture( 0, g_pTexture );

				(*Device)->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
                (*Device)->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
                (*Device)->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );

				(*Device)->SetStreamSource( 0, *g_pVB, 0, sizeof(CUSTOMVERTEX) );
				(*Device)->SetFVF( D3DFVF_CUSTOMVERTEX );
				(*Device)->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	 (*Device)->SetRenderState( D3DRS_ZENABLE, TRUE );

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// С„СѓРЅРєС†РёСЏ РѕС‚СЂРёСЃРѕРІРєРё РІ РѕРїРµСЂРµРґРµР»РµРЅРЅРѕР№ С‚РѕС‡РєРµ СЃРјР°СЃС€С‚Р°Р±РёСЂРѕРІР°РЅРЅРѕРіРѕ С‚РµРєСЃС‚Р° Р·Р°РґР°РЅРЅРѕРіРѕ С†РІРµС‚Р°
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void                   Draw_Text(float x,          // x position
								 float y,          // y position
								 float z,          // z position
								 float w,          // width of rect
								 float h,          // height of rect
								 char* t,          // text string
								 D3DCOLOR*  Color) // text color
{
	    if (!ZBuffer_Enable) 
           (*Device)->SetRenderState( D3DRS_ZENABLE, FALSE );
				D3DXMatrixIdentity(&Transform);	
	            D3DXMatrixIdentity(&Buff);

				D3DXMatrixRotationX(&Buff,D3DX_PI);
				D3DXMatrixMultiply(&Transform,&Transform,&Buff);

				D3DXMatrixScaling(&Buff,w/2,h/2,1);
				D3DXMatrixMultiply(&Transform,&Transform,&Buff);

				D3DXMatrixTranslation(&Buff,x,y,z);
				D3DXMatrixMultiply(&Transform,&Transform,&Buff);

				D3DXMatrixMultiply(&Transform,&Transform,&matWorld);

				(*Device)->SetTransform( D3DTS_WORLD, &Transform);
				
			Text_Sprite->Begin(D3DXSPRITE_OBJECTSPACE| D3DXSPRITE_ALPHABLEND  );
				(*Font)->DrawText( Text_Sprite, t, -1, NULL, 0|DT_LEFT , *Color );
			Text_Sprite->End();
			(*Device)->SetRenderState( D3DRS_ZENABLE, TRUE );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool           MouseEvent(unsigned short event_index,POINT m){
	curr_mouse_point = m;
	MouseEvent_More(m);
	if (event_index==1)
	{
		
	if ((m.x>pos_x)&&(m.x<pos_x+width)&&(m.y>pos_y)&&(m.y<(pos_y+height))){
		selected = true;
		TextColor = D3DCOLOR_XRGB(255,255,255);
	
		last_mouse_point =m;
		last_mouse_pitch.x = pos_x+width-m.x;
        last_mouse_pitch.y = -(pos_y+height -m.y);
		allow_shift = true;

		return true;
	    }
	selected = false;
	
	TextColor = D3DCOLOR_XRGB(200,200,240);
	return false;
	}
	else
	if ((event_index==2)&&(allow_shift)&&(selected)){
	  pos_x +=m.x-last_mouse_point.x;
	  pos_y +=m.y-last_mouse_point.y;
	  allow_shift = false;
	  Init();		
	}
	//РґР»СЏ РјРёРіР°РЅРёСЏ
	else if (!selected){
	if ((m.x>pos_x)&&(m.x<pos_x+width)&&(m.y>pos_y)&&(m.y<(pos_y+height))&&(!allow_shift)){
	BgColor = D3DCOLOR_XRGB(91,91,91);
	Init();
	}else{BgColor = D3DCOLOR_XRGB(21,21,21);
	Init();
	}}
	else if (selected)BgColor = D3DCOLOR_XRGB(51,51,51) ;
	return false;
}
void           MouseEvent_More(POINT m){}

void     Setup_SizePos(int x,int y,int w,int h){
	pos_x  = x;
	pos_y  = y;
	width  = w;
	height = h;
	Resize();
}
void     Setup_Color(D3DCOLOR  Back, D3DCOLOR Text, D3DCOLOR Bg){
    BackColor = Back;
    TextColor = Text;
    BgColor   = Bg;
	Resize();
}
void     SetText(char *text){Text=text;}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////   С„СѓРЅРєС†РёСЏ РїСЂРѕРІРµСЂРєРё РїРµСЂРµСЃРµС‡РµРЅРёР№ СЃ СЌР»РµРјРµРЅС‚Р°РјРё gui | РґР»СЏ СЂР°Р·РЅРѕРіРѕ РєРѕР»РёС‡РµСЃС‚РІР° РєРЅРѕРїРѕРє РЅСѓР¶РЅРѕ РїРµСЂРµРіСЂСѓР·РёС‚СЊ С„СѓРЅРєС†РёСЋ
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool    Check_Intersect(D3DXVECTOR3 *vRayDir,D3DXVECTOR3 *vRayOrig){
    // РїРѕРєР° РїСЂРѕРІРµСЂРєР° РїРµСЂРµСЃРµС‡РµРЅРёСЏ СЃ Р±Р°Р·РѕРІС‹Рј
  /*

  BOOL D3DXIntersectTri(
  CONST D3DXVECTOR3 *p0,
  CONST D3DXVECTOR3 *p1,
  CONST D3DXVECTOR3 *p2,
  CONST D3DXVECTOR3 *pRayPos,
  CONST D3DXVECTOR3 *pRayDir,
  FLOAT *pU,
  FLOAT *pV,
  FLOAT *pDist
);
  */
	 //Draw_Rect(pos_x-5050,pos_y,10052,width,height);

	D3DXVECTOR3 vPickRayDir = *vRayDir;
	D3DXVECTOR3 vPickRayOrig = *vRayOrig;

	D3DXVECTOR3 x1 = D3DXVECTOR3(pos_x,pos_y,0);
	D3DXVECTOR3 x2 = D3DXVECTOR3(pos_x+width,pos_y,0);
	D3DXVECTOR3 x3 = D3DXVECTOR3(pos_x+width,pos_y+height,0);
	D3DXVECTOR3 x4 = D3DXVECTOR3(pos_x,pos_y+height,0);

	float distance = 0.0;

	bool result = false;

	// С‚СЂР°РЅСЃС„РѕСЂРјР°С†РёРё РґР»СЏ РїСЂСЏРјРѕСѓРіРѕР»СЊРЅРёРєР° | РїРѕР»СѓС‡Р°РµРј РІРѕР»С€РµР±РЅСѓСЋ РјР°С‚СЂРёС†Сѓ
	  D3DXMatrixIdentity(&Transform);	

	  // РЅСѓР¶РЅРѕ Р»Рё РїРѕРІРѕСЂР°С‡РёРІР°С‚СЊ?
	  D3DXMatrixIdentity(&Buff);
	  D3DXMatrixRotationX(&Buff,D3DX_PI);
	  D3DXMatrixMultiply(&Transform,&Transform,&Buff);

	  //D3DXMatrixScaling(&Buff,width,height,1);
	 // D3DXMatrixMultiply(&Transform,&Transform,&Buff);

	  D3DXMatrixTranslation(&Buff,pos_x-5050,pos_y,10052);
	  D3DXMatrixMultiply(&Transform,&Transform,&Buff);

	  D3DXMatrixMultiply(&Transform,&Transform,&matWorld);


	  D3DXMatrixInverse(&Transform,NULL,&Transform);
      D3DXVec3TransformCoord(&vPickRayOrig,&vPickRayOrig,&Transform);
      D3DXVec3TransformNormal(&vPickRayDir, &vPickRayDir, &Transform);

	  result = D3DXIntersectTri(&x1,&x2,&x3,&vPickRayOrig,&vPickRayDir,NULL,NULL,&distance);
	  if (!result)
		   result = D3DXIntersectTri(&x1,&x3,&x4,&vPickRayOrig,&vPickRayDir,NULL,NULL,&distance);
	  
	  return result;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT        Init() {

	 g_pTexture = NULL;
	 D3DXCreateTextureFromFile( *Device, "button_2.png", &g_pTexture );

	
	D3DXCreateSprite(*Device,&Text_Sprite);
	SAFE_RELEASE(g_pVB);
	g_pVB = new LPDIRECT3DVERTEXBUFFER9();

	BgColor = D3DCOLOR_XRGB(255,242,0);
    CUSTOMVERTEX vertices[] =
    {
		{ 0        , 0+10    , 0.0f, 1.0f, BgColor  , 0, 1 }, 
        { 0        , 0       , 0.0f, 1.0f, BgColor  , 0, 0 }, 
        { 0+10     , 0+10    , 0.0f, 1.0f, BgColor	, 1, 1 },
		{ 0+10     , 0       , 0.0f, 1.0f, BgColor	, 1, 0 },
    };

    if( FAILED( (*Device)->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

	VOID* pVertices;
    if( FAILED( (*g_pVB)->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof(vertices) );
	(*g_pVB)->Unlock();

	
Init_More();
}
void        Resize() {
	if (g_pVB!=NULL){	
	      /*        
		CUSTOMVERTEX vertices[] =
			{
				{ 0        , 0+10        , -1.0f, -1.0f, BgColor  }, 
				{ 0        , 0           , -1.0f, -1.0f, BgColor	 }, 
				{ 0+10     , 0+10        , -1.0f, -1.0f, BgColor	 },   
				{ 0+10     , 0           , -1.0f, -1.0f, BgColor	 },
			};

		VOID* pVertices;
		if( FAILED( (*g_pVB)->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
			return E_FAIL;
		memcpy( pVertices, vertices, sizeof(vertices) );
		(*g_pVB)->Unlock();*/
		return;

	}else Init();
}
void           Init_More(){}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///// 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void           Select(){	selected = true;	TextColor = D3DCOLOR_XRGB(255,255,255);}
void           Deselect(){selected = false;     TextColor = D3DCOLOR_XRGB(200,200,240);}

void set_Main_Matrix(D3DXMATRIX          mat){
	matWorld = mat;
}
void ZENABLE(bool ze){
   ZBuffer_Enable = ze;
}
//#################################################################################################################
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                     РљРћРќРЎРўР РЈРљРўРћР 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CLREngi_UI_BaseUI(LPDIRECT3DDEVICE9 *D,char *nm){
   
   pos_x  = 0;
   pos_y  = 0;
   width  = 5;
   height = 15;
   Device = D;
   g_pVB=NULL;

   Font=NULL; 
   selected = false;
   TextColor =D3DCOLOR_XRGB(200,200,240);


   Name = nm;

   allow_shift = false;
   BgColor = D3DCOLOR_XRGB(51,51,51);
   BackColor = D3DCOLOR_XRGB(200,200,200);

   //SAFE_RELEASE(FontName);
   FontName = new LPD3DXFONT();
   D3DXCreateFont( (*Device), 10,5, FW_ULTRALIGHT, 0, FALSE, 
		                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                 DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman"), 
		                 FontName );

  
   
 //  SAFE_RELEASE(Font);
/*	Font = new LPD3DXFONT();
   D3DXCreateFont( (*Device), 15,6, FW_BOLD, 0, FALSE, 
		                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
		                 DEFAULT_PITCH | FF_DONTCARE, TEXT("Courier "), 
		                 Font );
  */ 
   visible = true;
   ZBuffer_Enable = true;



   //Init();
   }
CLREngi_UI_BaseUI(){


};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                       Р”Р•РЎРўР РЈРљРўРћР 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~CLREngi_UI_BaseUI(void)
   {
	   SAFE_RELEASE(g_pVB);
	   SAFE_RELEASE(Font);
	   SAFE_RELEASE(FontName);
   }

};

#endif 

